﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.Role;
using JESAI.Application.Abstractions.Dtos;
using JESAI.Framework.Filters;
using System;
using System.Collections.Generic;
using JESAI.Application.Abstractions.Admin.Dtos.User;

namespace Bucket.Admin.Web.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [ApiController]
    [Authorize("permission")]
    [IgnoreResultHandlerAttribute]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="roleService"></param>
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 查询所有角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Role/QueryAllRoles")]
        public BaseOutput<object> QueryAllRoles([FromQuery] QueryRolesInput input)
        {
            var list = _roleService.QueryAllRoles(input);

            return new BaseOutput<object> { Data = list };
        }
        /// <summary>
        /// 查询可用角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Role/QueryRoles")]
        public BasePageOutput<List<QueryRoleDto>> QueryRoles([FromQuery] QueryRolesPageInput input)
        {
            var result = _roleService.QueryRoles(input);
            return result;
        }
        /// <summary>
        /// 查询角色权限信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Role/QueryRoleInfo")]
        public BaseOutput<object> QueryRoleInfo([FromQuery] QueryRoleInfoInput input)
        {
            var model = _roleService.QueryRoleInfo(input.RoleId);
            var apiList = _roleService.QueryApiByRoleId(input.RoleId);
            var menuList = _roleService.QueryMenuByRoleId(input.RoleId);
            return new BaseOutput<object> { Data = new { RoleInfo = model, MenuList = menuList.Select(m => m.MenuId), ApiList = apiList.Select(a => a.ApiId) } };
        }
        /// <summary>
        /// 设置角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Role/SetRole")]
        public BaseOutput SetRole([FromBody] SetRoleInput input)
        {
            _roleService.UpdateOrInsert(input);

            return new BaseOutput { };
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/Role/DeleteRole")]
        public BaseOutput DeleteRole(long id)
        {
            _roleService.DeleteRole(id);

            return new BaseOutput { };
        }

        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/Role/DeleteRoles")]
        public BaseOutput DeleteRoles(long[] ids)
        {
            _roleService.DeleteRoles(ids);

            return new BaseOutput { };
        }
    }
}